import random

from joblib import load


def word2sentence_similar(perm_docs, primary_doc_id, spl_num=5):
    sentence = (load(r"../data/20ng/data_20ng_SBERT_embedding.pkl"))['doc_sentences']
    word_id2word = (load(r"../data/20ng/bow2word.pkl"))["id2word"]
    spl_indxs = random.sample(range(0, len(primary_doc_id) + 1), spl_num)
    spl_indxs.sort()
    spl_perm_docs = []
    slp_primary_doc_id = []
    for i in spl_indxs:
        spl_perm_docs.append(perm_docs[i])
        slp_primary_doc_id.append(primary_doc_id[i])

    wordid2word_docs = []
    for doc in spl_perm_docs:
        words = []
        for word_id in doc.squeeze():
            words.append(word_id2word[word_id])
        wordid2word_docs.append(words)

    sbt_docs = []
    for t in slp_primary_doc_id:
        sbt_docs.append(sentence[t])
    re = []
    for doc_id in range(len(slp_primary_doc_id)):
        acc = 0
        sentence = sbt_docs[doc_id]
        len_ = len(wordid2word_docs[doc_id])
        for word in wordid2word_docs[doc_id]:
            if word in sentence:
                acc = acc + 1
        re.append(tuple((slp_primary_doc_id[doc_id], round(acc / len_, 2))))
        print("doc原始id:{a}, word原文重现概率为: {b}".format(a=slp_primary_doc_id[doc_id], b=round(acc / len_, 2)))
    return re
